import Vue from "vue";
import Router from "vue-router";

Vue.use(Router);

const router = new Router({
  routes: [
    {
      path: "/",
      name: "/",
      component: () => import("@/components/login/loading"),
      hidden: true
    },
    {
      path: "/home",
      name: "home",
      meta: { title: "首页", icon: "icon iconfont icon-shouye" },
      component: () => import("@/views/home")
    },
    {
      path: "/fenClass",
      name: "fenClass",
      meta: { title: "分类", icon: "icon iconfont icon-fenlei-xuanzhong" },
      component: () => import("@/views/fenClass")
    },
    // 购物车-yqq
    {
      path: "/shopping",
      name: "shopping",
      meta: {
        title: "购物车",
        isLogin: true,
        shopIndex: 0,
        icon: "icon iconfont icon-gouwuche"
      },
      component: () => import("@/views/shopping")
    },
    {
      path: "/shoppings",
      name: "shoppings",
      meta: {
        isLogin: true,
        title: "购物车页",
        shopIndex: 1
      },
      component: () => import("@/components/shopping_s/shoppings"),
      hidden: true
    },
    {
      path: "/confirmOrder",
      name: "confirmOrder",
      meta: {
        title: "确认订单页",
        shopIndex: 2
      },
      component: () => import("@/components/shopping_s/confirmOrder"),
      hidden: true
    },
    {
      path: "/payAway",
      name: "payAway",
      meta: {
        title: "支付方式页",
        shopIndex: 4
      },
      component: () => import("@/components/shopping_s/payAway"),
      hidden: true
    },
    {
      path: "/paySuccess",
      name: "paySuccess",
      meta: {
        title: "支付成功页",
        shopIndex: 5
      },
      component: () => import("@/components/shopping_s/paySuccess"),
      hidden: true
    },
    {
      path: "/my",
      name: "my ",
      meta: { title: "我的", isLogin: true, icon: "icon iconfont icon-wode" },
      component: () => import("@/views/my")
    },
    {
      path: "/login",
      name: "login",
      meta: { title: "登录页" },
      hidden: true,
      component: () => import("@/components/login")
    },
    {
      path: "/addAccount",
      name: "addAccount",
      hidden: true,
      meta: {
        title: "注册账号-手机验证"
      },
      component: () => import("@/components/login/addAccount")
    },
    {
      path: "/addAccount",
      name: "addAccount",
      hidden: true,
      meta: {
        title: "找回密码-手机验证"
      },
      component: () => import("@/components/login/addAccount")
    },
    {
      path: "/backpasword",
      name: "backpasword",
      hidden: true,
      meta: {
        title: "找回密码-修改密码/注册账号-设置密码"
      },
      component: () => import("@/components/login/addAccount/backpasword")
    },
    {
      path: "/classify",
      name: "二次分类页",
      hidden: true,
      component: () => import("@/views/fenClass/index_classify")
    },
    {
      path: "/details",
      name: "商品详情页",
      hidden: true,
      component: () => import("@/views/fenClass/details")
    },
    //-----------------韩金旗-------------------
    {
      path: "/site",
      name: "site",
      meta: {
        title: "收货地址页",
        shopIndex: 3,
        isLogin: true
      },
      component: () => import("@/views/my/site"),
      hidden: true
    },
    {
      path: "/xinsite",
      name: "xinsite",
      meta: { title: "添加收货地址页", isLogin: true },
      component: () => import("@/views/my/site/xinsite"),
      hidden: true
    },
    {
      path: "/sign",
      name: "sign",
      meta: { title: "签到", isLogin: true },
      component: () => import("@/views/my/sign"),
      hidden: true
    },
    {
      path: "/sets",
      name: "sets",
      meta: { title: "设置页", isLogin: true },
      component: () => import("@/views/my/sign/sets"),
      hidden: true
    },
    {
      path: "/getsd",
      name: "getsd",
      meta: { title: "个人信息页", isLogin: true },
      component: () => import("@/views/my/sign/sets/getsd"),
      hidden: true
    },
    {
      path: "/hsini",
      name: "hsini",
      meta: { title: "修改个人信息页", isLogin: true },
      component: () => import("@/views/my/sign/sets/getsd/hsini"),
      hidden: true
    },
    {
      path: "/recei",
      name: "recei",
      meta: { title: "待收货页", isLogin: true },
      component: () => import("@/views/my/recei"),
      hidden: true
    },
    {
      path: "/about",
      name: "about",
      meta: { title: "关于我们页", isLogin: true },
      component: () => import("@/views/my/about"),
      hidden: true
    },
    {
      path: "/colle",
      name: "colle",
      meta: { title: "我的收藏页", isLogin: true },
      component: () => import("@/views/my/colle"),
      hidden: true
    },
    {
      path: "/ntegr",
      name: "ntegr",
      meta: { title: "积分商城页", isLogin: true },
      component: () => import("@/views/my/ntegr"),
      hidden: true
    },
    {
      path: "/town",
      name: "town",
      meta: { title: "我的客服页", isLogin: true },
      component: () => import("@/views/my/town"),
      hidden: true
    },
    {
      path: "/index_value",
      hidden: true,
      component: () => import("@/views/fenClass/index_value")
    }
  ]
});

// // 路由拦截器
// to代表要去的路由指向,from是指从哪个路由跳转过来的,next是判断操作,如果为空,表示放行。
router.beforeEach((to, from, next) => {
  // console.log(to);
  // console.log(from);
  if (to.matched.some(item => item.meta.isLogin)) {
    // 验证是否需要登陆
    if (localStorage.getItem("token")) {
      // 查询本地存储信息是否已经登陆
      next();
    } else {
      next({
        path: "/login", // 未登录则跳转至login页面
        query: { path: to.fullPath } // 登陆成功后回到当前页面，这里传值给login页面，to.fullPath为当前点击的页面
      });
    }
  } else {
    next();
  }
});

export default router;
